# -*- coding: utf-8 -*-
# created on 2016/12/22


from mathsolver.functions.base import *
from mathsolver.functions.root.jiefangchen import JieFangChen
from mathsolver.functions.zhixian.base import default_symbols


# 待定系数法求直线方程
class DaiDingXiShuFa001(BaseFunction):
    def solver(self, *args):
        if len(args) == 2 and isinstance(args[0], BasePoint):
            if isinstance(args[1], BaseZhiXian) or isinstance(args[1], BaseZhiXians):
                name = ""
                point = args[0].sympify()
                eq = args[1].sympify()
            else:
                raise Exception('Try Error!')
        else:
            raise Exception('Try Error!')
        symx, symy = default_symbols(eq[0] - eq[1])
        expr = eq[0] - eq[1]
        new_expr = expr.subs({symx: point[0], symy: point[1]})
        step1 = JieFangChen().solver(BaseEq([new_expr, S.Zero]))
        self.steps += step1.steps
        jie = step1.output[0].value
        jie_keys = jie.keys()[0]
        answer = jie[jie_keys].args[0]
        new_eq = [eq[0].subs({jie_keys: answer}), eq[1].subs({jie_keys: answer})]
        self.steps.append(["", "直线的方程为 %s" % (BaseZhiXian({"name": name, "value": new_eq}).printing())])
        self.output.append(BaseZhiXian({"name": name, "value": new_eq}))
        return self


# 待定系数法求抛物线方程
class DaiDingXiShuFa005(BaseFunction):
    def solver(self, *args):
        if len(args) == 2 and isinstance(args[0], BasePoint):
            if isinstance(args[1], BasePaoWuXian) or isinstance(args[1], BasePaoWuXians):
                name = ""
                point = args[0].sympify()
                eqs = args[1].sympify()
                self.steps.append(["", "∵%s在抛物线上" % (BasePoint({"name": "", "value": point}).printing())])
                if len(eqs) == 1:
                    print(1)
                else:
                    new_eqs = []
                    for eq in eqs:
                        symx, symy = default_symbols(eq[0] - eq[1])
                        expr = eq[0] - eq[1]
                        new_expr = expr.subs({symx: point[0], symy: point[1]})
                        step1 = JieFangChen().solver(BaseEq([new_expr, S.Zero]))
                        self.steps += step1.steps
                        jie = step1.output[0].value
                        jie_keys = jie.keys()[0]
                        answer = jie[jie_keys].args[0]
                        new_eq = [eq[0].subs({jie_keys: answer}), eq[1].subs({jie_keys: answer})]
                        new_eqs.append({"name": "", "value": new_eq})
                        self.steps.append(
                            ["", "∴抛物线的方程为 %s" % (BasePaoWuXian({"name": name, "value": new_eq}).printing())])
                    self.output.append(BasePaoWuXians(new_eqs))
                    self.label.add("待定系数法求抛物线方程")
                    return self
            else:
                raise Exception('Try Error!')
        else:
            raise Exception('Try Error!')


class DaiDingXiShuFaQiuJieXiShi(BaseFunction):
    CLS = [DaiDingXiShuFa001, DaiDingXiShuFa005]

    def solver(self, *args):
        r = None
        for cl in DaiDingXiShuFaQiuJieXiShi.CLS:
            try:
                r = cl(verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r


if __name__ == '__main__':
    pass
    # print "DaiDingXiShuFaQiuJieXiShi"
    # for step in DaiDingXiShuFaQiuJieXiShi().solver(BasePoint({"name":"","value":[1,2]}),
    #                                                BaseZhiXian({"name":"", "value":["y", "a*x+3"]})).steps:
    #     print step[0], step[1]

    # for step in DaiDingXiShuFaQiuJieXiShi().solver(BasePoint({"name":"","value":[4, -2]}),
    #                                                BasePaoWuXians([{"name":"", "value":[y**2, a*x]},
    #                                                                {"name":"", "value":[x**2, b*y]}])).steps:
    #     print step[0], step[1]
